<?php
$access_level = 3;
$page_title = 'eTAT';
require_once 'library/Controller.php';

$groups = null;
if(isset($instructor) && isset($params->c)) {
  $again = newBtn('update_again','Refresh',array('type'=>'button','onclick'=>'updateFeeds()'));
  $course = $instructor->getCourse($params->c);
    $all = $course->getAllGroups();
      if($all) {
	    foreach($all as $a) {
		  $query = mysql_query("SELECT * FROM `schedule_parser` WHERE `group_id` = '$a->id'") or die(mysql_error());
		  while($r = mysql_fetch_object($query)) {
		    $groups[] = $r;
		  }
	    }
      } else {
	    die("There are no feeds to update. $again");
     }
} else {
  $query = mysql_query("SELECT * FROM `schedule_parser`") or die(mysql_error());
  while($r = mysql_fetch_object($query)) {
    $groups[] = $r;
  }
}

$total = 0;
if($groups) {
	foreach($groups as $g) {
		if($g->feed_type == 'svn') {
			$total = $total + parseSVN($g);
		} 
		if($g->feed_type == 'issue') {
			$total = $total + parseIssues($g);
		}
		if($g->feed_type == 'wiki') {
			$total = $total + parseWiki($g);
		}
		if($g->feed_type == 'forum') {
			$total = $total + parseForum($g);
		}
		//@TODO remaining feeds.
	}
}
if($total == 0) $total = 'none';
die(print "Update complete.<br> Total feeds updated: <b>$total</b>.<br>$again");

function parseSVN($data) {
	$parsed_count = 0;
	$group = new Group($data->group_id);
	$latest_rev = $data->latest_update;	
	$feed_url = "http://code.google.com/feeds/p/$data->code_name/svnchanges/basic";
	$p = new Parser();
	$p->parse($feed_url);
	$items = $p->getItems();
	for($x = count($items)-1; $x >= 0; $x--) {
		$i = $items[$x];
		$group_id = $data->group_id;
		$revision = substr(strrchr($i['ID'],'/'),1);
		$url = "http://code.google.com/p/$data->code_name/source/detail?r=$revision";
		$author_name = $i['AUTHOR']['NAME'];
		$author = $group->searchStudent($author_name);
		$full_comment = $i['TITLE'];
		$comment = substr($full_comment,strlen("Revision $revision:"));
		$content = $i['CONTENT'];
		$is_wiki = strpos($content,'/wiki/') != false? true : false;
		
		if($revision > $latest_rev && $author != false && $is_wiki == false) {
			$latest_rev = $revision;
			$query = mysql_query("INSERT INTO `svn_commits` VALUES (
				NULL , '$group_id', '$url', '$revision', '$author->id', '$comment', '$content');") or die(mysql_error());
			$parsed_count++;
		}
	}
	$query = mysql_query("UPDATE `schedule_parser` SET `latest_update` = '$latest_rev' WHERE `id` = '$data->id'") or die(mysql_error());
	return $parsed_count;
}

function parseIssues($data) {
	$parsed_count = 0;
	$group = new Group($data->group_id);
	$latest_date = $data->latest_date;
	$feed_url = "http://code.google.com/feeds/p/$data->code_name/issueupdates/basic";
	$p = new Parser();
	$p->parse($feed_url);
	$items = $p->getItems();
	for($x = count($items)-1; $x >= 0; $x--) {
		$i = $items[$x];
		$group_id = $data->group_id;
		$date = $i['UPDATED'];
		$full_id = substr($i['ID'],strpos($i['ID'],'basic')+6);
		$pos = strpos($full_id,'/')? strpos($full_id,'/') : strlen($full_id);
		$issue_id = substr($full_id,0,$pos);
		$update_id = substr($full_id,$pos+1)? substr($full_id,$pos+1): "NULL";
		$update_url = $update_id != "NULL"? "#c$update_id" : '';
		$url = "http://code.google.com/p/$data->code_name/issues/detail?id=$issue_id$update_url";
		$title = trim(substr($i['TITLE'],strpos($i['TITLE'],'"')+1),'")');
		$description = $i['CONTENT'];
		$author_name = $i['AUTHOR']['NAME'];
		$author = $group->searchStudent($author_name);
		
		if($latest_date == 0 || $date > $latest_date) {
			$latest_date = $date;
			$query = mysql_query("INSERT INTO `issue_tracking` VALUES (
				NULL , '$group_id', '$issue_id', '$update_id' , '$url', '$author->id', '$title', '$description');") 
				or die(mysql_error());
			$parsed_count++;
		}
	}
	$query = mysql_query("UPDATE `schedule_parser` SET `latest_date` = '$latest_date' WHERE `id` = '$data->id'") or die(mysql_error());
	return $parsed_count;
}

function parseWiki($data) {
	$parsed_count = 0;
	$group = new Group($data->group_id);
	$latest_rev = $data->latest_update;	
	$feed_url = "http://code.google.com/feeds/p/$data->code_name/svnchanges/basic?path=/wiki/";
	$p = new Parser();
	$p->parse($feed_url);
	$items = $p->getItems();
	for($x = count($items)-1; $x >= 0; $x--) {
		$i = $items[$x];
		$group_id = $data->group_id;
		$revision = substr(strrchr($i['ID'],'/'),1);
		$url = "http://code.google.com/p/$data->code_name/source/detail?r=$revision";
		$author_name = $i['AUTHOR']['NAME'];
		$author = $group->searchStudent($author_name);
		$full_comment = $i['TITLE'];
		$comment = substr($full_comment,strlen("Revision $revision:"));
		$content = $i['CONTENT'];
		$is_wiki = strpos($content,'/wiki/') != false? true : false;
		
		if($revision > $latest_rev && $author != false) {
			$latest_rev = $revision;
			$query = mysql_query("INSERT INTO `wiki` VALUES (
				NULL , '$group_id', '$url', '$revision', '$author->id', '$comment', '$content');") or die(mysql_error());
			$parsed_count++;
		} 
	}
	$query = mysql_query("UPDATE `schedule_parser` SET `latest_update` = '$latest_rev' WHERE `id` = '$data->id'") or die(mysql_error());
	return $parsed_count;
}

function parseForum($data) {
	$parsed_count = 0;
	$group = new Group($data->group_id);
	$latest_rev = $data->latest_update;	
	$num = $latest_rev == 0? 500 : 50;
	$feed_url = "http://groups.google.com/group/$data->groups_name/feed/atom_v1_0_msgs.xml?num=$num";
	$p = new Parser();
	$p->parse($feed_url);
	$items = $p->getItems();
	for($x = count($items)-1; $x >= 0; $x--) {
		$i = $items[$x];
		$group_id = $data->group_id;
		$date = $i['UPDATED'];
		$author = $group->searchStudent($author_name);
		$url = $i['ID'];
		$title = $i['TITLE'];
		$author_name = $i['AUTHOR']['NAME'];
		$summary = $i['SUMMARY'];
		
		if($date > $latest_date && $author != false) {
			$latest_rev = $revision;
			$query = mysql_query("INSERT INTO `forum` VALUES (
				NULL , '$group_id', '$date', '$author->id', '$url', '$title', '$summary');") or die(mysql_error());
			$parsed_count++;
		} 
	}
	$query = mysql_query("UPDATE `schedule_parser` SET `latest_date` = '$latest_date' WHERE `id` = '$data->id'") or die(mysql_error());
	return $parsed_count;
}